/**
 * 
 */
package org.log4jconfig.xml.editor.contentassist.delegate;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.core.Flags;
import org.eclipse.jdt.core.IField;
import org.eclipse.jdt.core.IType;
import org.eclipse.jdt.core.Signature;
import org.eclipse.jdt.ui.ISharedImages;
import org.eclipse.wst.xml.ui.internal.contentassist.ContentAssistRequest;
import org.log4jconfig.xml.editor.utils.JdtUtils;

/**
 * @author Xi
 * 
 */
@SuppressWarnings("restriction")
public class LevelContentAssistDelegatedProcessor extends
		AbstractContentAssistDelegatedProcessor {

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.wst.xml.ui.internal.contentassist.XMLContentAssistProcessor
	 * #addAttributeValueProposals
	 * (org.eclipse.wst.xml.ui.internal.contentassist.ContentAssistRequest)
	 */
	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.log4jconfig.xml.editor.contentassist.IContentAssistDelegatedProcessor
	 * #
	 * addAttributeValueProposals(org.eclipse.wst.xml.ui.internal.contentassist.
	 * ContentAssistRequest)
	 */
	public void addAttributeValueProposals(
			ContentAssistRequest contentAssistRequest, String attributeName,
			String attributeCurrentValue, String attributeMatchValue)
			throws Exception {

		// content assist for the 'additivity' parameter value
		// FIXME : check the class attribute for the node, and use
		// 'org.apache.log4j.Level' as type name if none is declared
		// FIXME : parameter type must match declaring type or hierarchy !
		// FIXME : sort by values...
		if ("value".equals(attributeName)) {
			IType type = JdtUtils
					.getTypeFromCurrentProject("org.apache.log4j.Level");
			List<IField> values = new ArrayList<IField>();
			String typeSignature = Signature.createTypeSignature(type
					.getFullyQualifiedName(), true);
			for (IField field : type.getFields()) {

				if (Flags.isFinal(field.getFlags())
						&& Flags.isStatic(field.getFlags())
						&& Flags.isPublic(field.getFlags())
						&& !Flags.isDeprecated(field.getFlags())
						&& field.getTypeSignature().equals(typeSignature)) {
					values.add(field);
				}

			}
			for (IField value : values) {
				String doc = value.getAttachedJavadoc(null);
				if (doc != null) {
					addCompletionProposal(contentAssistRequest, value
							.getElementName(), doc, attributeCurrentValue,
							attributeMatchValue, ISharedImages.IMG_FIELD_PUBLIC);
				} else {
					addCompletionProposal(contentAssistRequest, value
							.getElementName(), attributeCurrentValue,
							attributeMatchValue, ISharedImages.IMG_FIELD_PUBLIC);
				}
			}
		}

	}

}
